<?php

namespace Labirynt\CmsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Util\Inflector;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Filter\Transformation;

/**
 * Drawing
 *
 * @ORM\Table(name="drawing")
 * @ORM\Entity(repositoryClass="Labirynt\CmsBundle\Repository\DrawingRepository")
 * @ORM\HasLifecycleCallbacks
 * @UniqueEntity("date")
 */
class Drawing
{
	private $temp;

	/**
	 * @var integer
	 *
	 * @ORM\Column(name="id", type="integer")
	 * @ORM\Id
	 * @ORM\GeneratedValue(strategy="AUTO")
	 */
	private $id;

	/**
	 * @var integer
	 *
	 * @ORM\Column(name="number", type="integer")
	 */
	private $number;

	/**
	 * @var \DateTime
	 *
	 * @ORM\Column(name="date", type="date")
	 */
	private $date;

	/**
	 * @var string
	 *
	 * @ORM\Column(name="filename", type="string", length=255)
	 */
	private $filename;

	/**
	 * @var integer
	 *
	 * @ORM\Column(name="file_size", type="integer")
	 */
	private $fileSize;

	/**
	 * @Assert\Image(maxSize="1M", minWidth=2000, minHeight=1400)
	 */
	private $file;


	public function __toString()
	{
		return ($this->filename === null) ? ' Nowy rysunek ' : $this->filename;
	}

	/**
	 * Sets file.
	 *
	 * @param UploadedFile $file
	 */
	public function setFile(UploadedFile $file = null)
	{
		$this->file = $file;
		
		// check if we have an old image path
		if (is_file($this->getAbsolutePath()))
		{
			// store the old name to delete after the update
			$this->temp = $this->getAbsolutePath();
		}
		else
		{
			$this->filename = 'initial';
		}
	}

	/**
	 * @ORM\PrePersist()
	 * @ORM\PreUpdate()
	 */
	public function preUpload()
	{
		if (null !== $this->getFile())
		{
			$name = 'tarkawian-366-obrotow-'.$this->getDate()->format('dmy').'.'.$this->getFile()->guessExtension();
			$this->filename = $name;
			$this->fileSize = $this->getFile()->getSize();
		}
	}

	/**
	 * @ORM\PostPersist()
	 * @ORM\PostUpdate()
	 */
	public function upload()
	{
		if (null === $this->getFile())
		{
			return;
		}

		// check if we have an old image
		if (isset($this->temp))
		{
			// delete the old image
			unlink($this->temp);
			// clear the temp image path
			$this->temp = null;
		}
		
		$extenstion = $this->getFile()->guessExtension();

		// you must throw an exception here if the file cannot be moved
		// so that the entity is not persisted to the database
		// which the UploadedFile move() method does
		$this->getFile()->move($this->getUploadRootDir(), $this->filename);
		
		$imagine = new Imagine();
		list($width, $height) = getimagesize($this->getAbsolutePath());
		$ratio = $width / $height;
		
		// small size
		$transformation = new Transformation();
		
		$path = $this->getUploadRootDir().'/'.'tarkawian-366-obrotow-'.$this->getDate()->format('dmy').'_s.'.$extenstion;
		$transformation
			->thumbnail(new Box(300, 210))
			->save($path);
		
		$transformation->apply($imagine->open($this->getAbsolutePath()));
		
		// medium size
		$transformation = new Transformation();
		
		$path = $this->getUploadRootDir().'/'.'tarkawian-366-obrotow-'.$this->getDate()->format('dmy').'_m.'.$extenstion;
		$transformation
			->thumbnail(new Box(1000, 700))
			->save($path);
		
		$transformation->apply($imagine->open($this->getAbsolutePath()));
		
		// large size
		$transformation = new Transformation();
		
		$path = $this->getUploadRootDir().'/'.'tarkawian-366-obrotow-'.$this->getDate()->format('dmy').'_l.'.$extenstion;
		$transformation
			->thumbnail(new Box(2000, 1400))
			->save($path);
		
		$transformation->apply($imagine->open($this->getAbsolutePath()));

		$this->setFile(null);
	}

	/**
	 * @ORM\PreRemove()
	 */
	public function storeFilenameForRemove()
	{
		$extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
		$basepath = basename($this->getAbsolutePath(), '.'.$extension);
		
		$this->temp = $this->getAbsolutePath();
		$this->small_name = $this->getUploadRootDir().'/'.$basepath.'_s.'.$extension;
		$this->medium_name = $this->getUploadRootDir().'/'.$basepath.'_m.'.$extension;
		$this->large_name = $this->getUploadRootDir().'/'.$basepath.'_l.'.$extension;
	}

	/**
	 * @ORM\PostRemove()
	 */
	public function removeUpload()
	{
		if (isset($this->temp))
		{
			@unlink($this->temp);
			@unlink($this->small_name);
			@unlink($this->medium_name);
			@unlink($this->large_name);
		}
	}

	/**
	 * Get file.
	 *
	 * @return UploadedFile
	 */
	public function getFile()
	{
		return $this->file;
	}

	public function getAbsolutePath()
	{
		return null === $this->filename ? null : $this->getUploadRootDir().'/'.$this->filename;
	}

	public function getWebPath($size = 'original')
	{
		$path = null;
		
		$extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
		$basepath = basename($this->getAbsolutePath(), '.'.$extension);
		
		$this->temp = $this->getAbsolutePath();
		
		switch ($size)
		{
			case 'small':
				$path = $this->getUploadDir().'/'.$basepath.'_s.'.$extension;
				break;
			case 'medium':
				$path = $this->getUploadDir().'/'.$basepath.'_l.'.$extension;
				break;
			case 'large':
				$path = $this->getUploadDir().'/'.$basepath.'_l.'.$extension;
				break;
			default:
				$path = $this->getUploadDir().'/'.$this->filename;
				break;
		}
		return null === $this->filename ? null : $path;
	}

	public function getUploadRootDir()
	{
		// the absolute directory path where uploaded
		// documents should be saved
		return __DIR__.'/../../../../web/'.$this->getUploadDir();
	}

	public function getUploadDir()
	{
		// get rid of the __DIR__ so it doesn't screw up
		// when displaying uploaded doc/image in the view.
		return 'uploads/drawings';
	}

	/**
	 * Get id
	 *
	 * @return integer 
	 */
	public function getId()
	{
		return $this->id;
	}

	/**
	 * Set date
	 *
	 * @param \DateTime $date
	 * @return Drawing
	 */
	public function setDate($date)
	{
		$this->date = $date;
	
		return $this;
	}

	/**
	 * Get date
	 *
	 * @return \DateTime 
	 */
	public function getDate()
	{
		return $this->date;
	}

	/**
	 * Set filename
	 *
	 * @param string $filename
	 * @return Drawing
	 */
	public function setFilename($filename)
	{
		$this->filename = $filename;
	
		return $this;
	}

	/**
	 * Get filename
	 *
	 * @return string 
	 */
	public function getFilename()
	{
		return $this->filename;
	}

	/**
	 * Set fileSize
	 *
	 * @param integer $fileSize
	 * @return Drawing
	 */
	public function setFileSize($fileSize)
	{
		$this->fileSize = $fileSize;
	
		return $this;
	}

	/**
	 * Get fileSize
	 *
	 * @return integer 
	 */
	public function getFileSize()
	{
		return $this->fileSize;
	}

	/**
	 * Set number
	 *
	 * @param integer $number
	 * @return Drawing
	 */
	public function setNumber($number)
	{
		$this->number = $number;
	
		return $this;
	}

	/**
	 * Get number
	 *
	 * @return integer
	 */
	public function getNumber()
	{
		return $this->number;
	}
}